package com.distribution.system.controller;

import com.alibaba.fastjson2.JSONObject;
import com.distribution.common.core.controller.BaseController;
import com.distribution.common.core.page.TableDataInfo;
import com.distribution.system.domain.bo.KuaidialAreas;
import com.distribution.system.domain.bo.KuaidialAreasJoin;
import com.distribution.system.domain.bo.ZoningAreas;
import com.distribution.system.service.CmccDaCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @author Liping Huo
 * @date 2023/5/23 15:53
 */
@RestController
@RequestMapping(value = "/cmccDaCode")
@Slf4j
public class CmccDaCodeController extends BaseController {

    @Resource
    CmccDaCodeService cmccDaCodeService;

    /**
     * 三级联动接口
     */
    @PostMapping("/zoningProvinceList")
    @ResponseBody
   String getZoningProvinceList() {
        List<ZoningAreas> areas = cmccDaCodeService.getZoningProvinceList();
        return getAreasJson(areas);
    }

    /**
     * 三级联动接口
     */
    @PostMapping("/zoningCityList")
    @ResponseBody
    String getZoningCityList() {
        List<ZoningAreas> areas = cmccDaCodeService.getZoningCityList();
        return getAreasJson(areas);
    }
    /**
     * 三级联动接口
     */
    @PostMapping("/zoningCountyList")
    String getZoningCountyList() {
        List<ZoningAreas> areas = cmccDaCodeService.getZoningCountyList();
        return getAreasJson(areas);
    }

    /**
     * 二级联动接口
     */
    @PostMapping("/kuaidialProvinceList")
    String getKuaidialProvinceList() {
        List<KuaidialAreas> areas = cmccDaCodeService.getKuaidialProvinceList();
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        for (KuaidialAreas area : areas) {
            objectObjectHashMap.put(area.getKuaidialCode(),area.getKuaidial());
        }
        JSONObject jsonObject = new JSONObject(objectObjectHashMap);
        return jsonObject.toString();
    }

    /**
     * 二级联动接口
     */
    @PostMapping("/kuaidialCityList")
    String getKuaidialCityList() {
        List<KuaidialAreas> areas = cmccDaCodeService.getKuaidialCityList();
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        for (KuaidialAreas area : areas) {
            objectObjectHashMap.put(area.getKuaidialCode(),area.getKuaidial());
        }
        JSONObject jsonObject = new JSONObject(objectObjectHashMap);
        return jsonObject.toString();
    }



    /**
     * 三级联动接口
     */
    @PostMapping("/kuaidialByName")
    String getKuaidialByName(@RequestBody String area) {
        String[] split = area.split(",");
        KuaidialAreasJoin kuaidialProvinceByName  =  cmccDaCodeService.getKuaidialProvinceByName(split[0]);
        KuaidialAreasJoin  kuaidialCityByName  =  cmccDaCodeService.getKuaidialCityByName(kuaidialProvinceByName,split[1]);
        if (Objects.isNull(kuaidialCityByName)){
            KuaidialAreasJoin kuaidialAreasJoin = new KuaidialAreasJoin();
            kuaidialAreasJoin.setUpKuaidial(kuaidialProvinceByName.getKuaidial());
            kuaidialAreasJoin.setUpKuaidialCode(kuaidialProvinceByName.getKuaidialCode());
            kuaidialAreasJoin.setKuaidial(kuaidialProvinceByName.getKuaidial());
            kuaidialAreasJoin.setKuaidialCode(kuaidialProvinceByName.getKuaidialCode()+"0");
            JSONObject from = JSONObject.from(kuaidialAreasJoin);
            return from.toString();
        }
        JSONObject from = JSONObject.from(kuaidialCityByName);
        return from.toString();
    }





    private static String getAreasJson(List<ZoningAreas> areas) {
        HashMap<String, String> objectObjectHashMap = new HashMap<>();
        for (ZoningAreas area : areas) {
            objectObjectHashMap.put(area.getZoningCode(),area.getZoning());
        }
        JSONObject jsonObject = new JSONObject(objectObjectHashMap);
        return jsonObject.toString();
    }


    /**
     * 三级联动接口
     */
    @PostMapping("/zoningCodeProvince")
    @ResponseBody
    TableDataInfo getProvince() {
        List<ZoningAreas> areas = cmccDaCodeService.getProvince();
        return getDataTable(areas);
    }

    /**
     * 三级联动接口
     */
    @PostMapping("/zoningCodeCity")
    TableDataInfo getCity(@RequestBody String province) {
        List<ZoningAreas> areas = cmccDaCodeService.getCity(province);
        return getDataTable(areas);
    }

    /**
     * 三级联动接口
     */
    @PostMapping("/zoningCodeCounty")
    TableDataInfo getCounty(@RequestBody String city) {
        List<ZoningAreas> areas = cmccDaCodeService.getCounty(city);
        return getDataTable(areas);
    }

    /**
     * 二级联动接口
     */
    @PostMapping("/kuaidialCodeProvince")
    TableDataInfo getKuaidialProvince() {
        List<KuaidialAreas> areas = cmccDaCodeService.getKuaidialProvince();
        return getDataTable(areas);
    }

    /**
     * 二级联动接口
     */
    @PostMapping("/kuaidialCodeCity")
    TableDataInfo getKuaidialCity(@RequestBody String kuaidialProvince) {
        List<KuaidialAreas> areas = cmccDaCodeService.getKuaidialCity(kuaidialProvince);
        return getDataTable(areas);
    }


}
